import { platform } from "@tauri-apps/plugin-os";
import { TauriInvoker } from "../../tauriUtils/TauriInvoker";

const rgbaRegex = new RegExp('#(?<r>[0-9A-Fa-f]{1,2})(?<g>[0-9A-Fa-f]{1,2})(?<b>[0-9A-Fa-f]{1,2})')
export type HexColor = `#${string}`

export async function setDomBackgroundColor(color: HexColor, winLabel: string) {
    document.body.style.backgroundColor = color;
    const match = rgbaRegex.exec(color)
    if (match != null && match.groups != undefined) {
        const gp = match.groups;
        const r = parseInt(gp.r, 16);
        const g = parseInt(gp.g, 16);
        const b = parseInt(gp.b, 16);
        if (!Number.isNaN(r) && !Number.isNaN(g) && !Number.isNaN(b)) {
            if (platform() == 'macos') {
                await TauriInvoker.setBarColor({ r, g, b, a: 1 }, winLabel);
            }
        }
    }
}